package cn.iocoder.yudao.module.biz.controller.admin.customer.serviceUnit.team;

import cn.iocoder.yudao.module.biz.dal.dataobject.customer.serviceUnit.team.ServiceUnitTeamDO;
import cn.iocoder.yudao.module.biz.service.customer.serviceUnit.team.ServiceUnitTeamService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.customer.serviceUnit.team.vo.*;


@Tag(name = "管理后台 - 客户管理-服务单元-内部小组")
@RestController
@RequestMapping("/san/biz-customer-archive-service-unit-team")
@Validated
public class ServiceUnitTeamController {

    @Resource
    private ServiceUnitTeamService serviceUnitTeamService;

    @PostMapping("/create")
    @Operation(summary = "创建客户管理-服务单元-内部小组")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-team:create')")
    public CommonResult<Long> createServiceUnitTeam(@Valid @RequestBody ServiceUnitTeamSaveReqVO createReqVO) {
        return success(serviceUnitTeamService.createServiceUnitTeam(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户管理-服务单元-内部小组")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-team:update')")
    public CommonResult<Boolean> updateServiceUnitTeam(@Valid @RequestBody ServiceUnitTeamSaveReqVO updateReqVO) {
        serviceUnitTeamService.updateServiceUnitTeam(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户管理-服务单元-内部小组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-team:delete')")
    public CommonResult<Boolean> deleteServiceUnitTeam(@RequestParam("id") Long id) {
        serviceUnitTeamService.deleteServiceUnitTeam(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除客户管理-服务单元-内部小组")
                @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-team:delete')")
    public CommonResult<Boolean> deleteServiceUnitTeamList(@RequestParam("ids") List<Long> ids) {
        serviceUnitTeamService.deleteServiceUnitTeamListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户管理-服务单元-内部小组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-team:query')")
    public CommonResult<ServiceUnitTeamRespVO> getServiceUnitTeam(@RequestParam("id") Long id) {
        ServiceUnitTeamDO serviceUnitTeam = serviceUnitTeamService.getServiceUnitTeam(id);
        return success(BeanUtils.toBean(serviceUnitTeam, ServiceUnitTeamRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户管理-服务单元-内部小组分页")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-team:query')")
    public CommonResult<PageResult<ServiceUnitTeamRespVO>> getServiceUnitTeamPage(@Valid ServiceUnitTeamPageReqVO pageReqVO) {
        PageResult<ServiceUnitTeamDO> pageResult = serviceUnitTeamService.getServiceUnitTeamPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServiceUnitTeamRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户管理-服务单元-内部小组 Excel")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit-team:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportServiceUnitTeamExcel(@Valid ServiceUnitTeamPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceUnitTeamDO> list = serviceUnitTeamService.getServiceUnitTeamPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户管理-服务单元-内部小组.xls", "数据", ServiceUnitTeamRespVO.class,
                        BeanUtils.toBean(list, ServiceUnitTeamRespVO.class));
    }

}